{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "5beaa751-1ea2-467f-96b6-d29a436f001c",
   "metadata": {},
   "source": [
    "### FP16"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "778f1b3a-9a43-448f-8490-c1e5f1f6d44a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "finfo(resolution=0.001, min=-65504, max=65504, eps=0.000976562, smallest_normal=6.10352e-05, tiny=6.10352e-05, dtype=float16)"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import torch\n",
    "torch.finfo(torch.float16)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "e55197aa-b5af-4958-896d-a9cd5e4605a9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.141: 3.140625\n",
      "3.1415: 3.140625\n",
      "3.142: 3.142578125\n"
     ]
    }
   ],
   "source": [
    "# 把10进制数转化为 torch.float16\n",
    "num = 3.141\n",
    "num_fp16 = torch.tensor(num).half()\n",
    "print(f\"3.141: {num_fp16}\")\n",
    "\n",
    "num = 3.1415\n",
    "num_fp16 = torch.tensor(num).half()\n",
    "print(f\"3.1415: {num_fp16}\")\n",
    "\n",
    "num = 3.142\n",
    "num_fp16 = torch.tensor(num).half()\n",
    "print(f\"3.142: {num_fp16}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "e155cdb8-7a1d-426c-b834-65e52813abce",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.141: 3.140625\n",
      "二进制： 0100001001001000\n",
      "3.1415: 3.140625\n",
      "二进制： 0100001001001000\n",
      "3.142: 3.142578125\n",
      "二进制： 0100001001001001\n"
     ]
    }
   ],
   "source": [
    "# float16变成2进制\n",
    "import struct\n",
    "def float16_to_bin(num):\n",
    "    # 将float16数打包为2字节16位，使用struct.pack\n",
    "    packed_num = struct.pack('e', num)\n",
    "\n",
    "    # 解包打包后的字节以获取整数表示\n",
    "    int_value = struct.unpack('H', packed_num)[0]\n",
    "\n",
    "    # 将整数表示转换为二进制\n",
    "    binary_representation = bin(int_value)[2:].zfill(16)\n",
    "    return binary_representation\n",
    "\n",
    "num = 3.141\n",
    "num_fp16 = torch.tensor(num).half()\n",
    "print(f\"3.141: {num_fp16}\")\n",
    "binary_representation = float16_to_bin(num_fp16)\n",
    "print(f\"二进制： {binary_representation}\")  # 打印二进制表示\n",
    "\n",
    "num = 3.1415\n",
    "num_fp16 = torch.tensor(num).half()\n",
    "print(f\"3.1415: {num_fp16}\")\n",
    "binary_representation = float16_to_bin(num_fp16)\n",
    "print(f\"二进制： {binary_representation}\")  # 打印二进制表示\n",
    "\n",
    "num = 3.142\n",
    "num_fp16 = torch.tensor(num).half()\n",
    "print(f\"3.142: {num_fp16}\")\n",
    "binary_representation = float16_to_bin(num_fp16)\n",
    "print(f\"二进制： {binary_representation}\")  # 打印二进制表示"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "6aeadf20-ae44-4fa7-b73d-9e38c6e0c36a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "通过2进制转化后Float16值: 3.140625\n",
      "通过2进制转化后Float16值: 3.140625\n",
      "通过2进制转化后Float16值: 3.142578125\n"
     ]
    }
   ],
   "source": [
    "#　2进制变成float16\n",
    "def binary_to_float16(binary_string):\n",
    "    # 检查输入是否是有效的16位二进制字符串\n",
    "    if len(binary_string) != 16:\n",
    "        raise ValueError(\"输入的二进制字符串必须是16位长\")\n",
    "\n",
    "    # 提取组成部分：符号、指数、尾数\n",
    "    sign = int(binary_string[0])  # 符号位\n",
    "    exponent = int(binary_string[1:6], 2)  # 指数位\n",
    "    mantissa = int(binary_string[6:], 2) / 1024.0  # 尾数位，除以2的10次方（即1024）以获得10位精度\n",
    "\n",
    "    # 根据符号、指数和尾数计算float16值\n",
    "    value = (-1) ** sign * (1 + mantissa) * 2 ** (exponent - 15)\n",
    "    return value\n",
    "\n",
    "# 10进制3.141对应float16：3.1406\n",
    "binary_representation = \"0100001001001000\"\n",
    "# 将二进制表示转换为float16\n",
    "float16_value = binary_to_float16(binary_representation)\n",
    "print(\"通过2进制转化后Float16值:\", float16_value)\n",
    "# 结果：\n",
    "\n",
    "# 10进制3.1415对应float16：3.1406\n",
    "binary_representation = \"0100001001001000\"\n",
    "# 将二进制表示转换为float16\n",
    "float16_value = binary_to_float16(binary_representation)\n",
    "print(\"通过2进制转化后Float16值:\", float16_value)\n",
    "\n",
    "# 10进制3.142对应float16：3.1426\n",
    "binary_representation = \"0100001001001001\"\n",
    "# 将二进制表示转换为float16\n",
    "float16_value = binary_to_float16(binary_representation)\n",
    "print(\"通过2进制转化后Float16值:\", float16_value)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4f5c7e7d-c01c-4ed8-a1f7-e9d582f8c799",
   "metadata": {},
   "source": [
    "### BF16"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "8c4bb38f-c408-4f85-a5ec-d6fba45e52c5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "finfo(resolution=0.01, min=-3.38953e+38, max=3.38953e+38, eps=0.0078125, smallest_normal=1.17549e-38, tiny=1.17549e-38, dtype=bfloat16)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.finfo(torch.bfloat16)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ed76269d-2b88-45fd-a862-b572382aada9",
   "metadata": {},
   "source": [
    "### FP32"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "aa294bf7-d748-4cb4-819b-c8a8ba8d4257",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "finfo(resolution=1e-06, min=-3.40282e+38, max=3.40282e+38, eps=1.19209e-07, smallest_normal=1.17549e-38, tiny=1.17549e-38, dtype=float32)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.finfo(torch.float32)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f6e76055-8e76-4cff-8172-5f350a9bf218",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
